<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: PacManUiBuilder</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.ui</a> ]
    </div>

    <h1>Coverage Summary for Class: PacManUiBuilder (nl.tudelft.jpacman.ui)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">PacManUiBuilder</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 9)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 30)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.ui;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import java.util.HashMap;
        <i>4</i>&nbsp;import java.util.LinkedHashMap;
        <i>5</i>&nbsp;import java.util.Map;
        <i>6</i>&nbsp;
        <i>7</i>&nbsp;import nl.tudelft.jpacman.game.Game;
        <i>8</i>&nbsp;import nl.tudelft.jpacman.ui.ScorePanel.ScoreFormatter;
        <i>9</i>&nbsp;
        <i>10</i>&nbsp;/**
        <i>11</i>&nbsp; * Builder for the JPac-Man UI.
        <i>12</i>&nbsp; *
        <i>13</i>&nbsp; * @author Jeroen Roosen
        <i>14</i>&nbsp; */
        <b class="nc"><i>15</i>&nbsp;public class PacManUiBuilder {</b>
        <i>16</i>&nbsp;
        <i>17</i>&nbsp; /**
        <i>18</i>&nbsp; * Caption for the default stop button.
        <i>19</i>&nbsp; */
        <i>20</i>&nbsp; private static final String STOP_CAPTION = &quot;Stop&quot;;
        <i>21</i>&nbsp;
        <i>22</i>&nbsp; /**
        <i>23</i>&nbsp; * Caption for the default start button.
        <i>24</i>&nbsp; */
        <i>25</i>&nbsp; private static final String START_CAPTION = &quot;Start&quot;;
        <i>26</i>&nbsp;
        <i>27</i>&nbsp; /**
        <i>28</i>&nbsp; * Map of buttons and their actions.
        <i>29</i>&nbsp; */
        <i>30</i>&nbsp; private final Map&lt;String, Action&gt; buttons;
        <i>31</i>&nbsp;
        <i>32</i>&nbsp; /**
        <i>33</i>&nbsp; * Map of key events and their actions.
        <i>34</i>&nbsp; */
        <i>35</i>&nbsp; private final Map&lt;Integer, Action&gt; keyMappings;
        <i>36</i>&nbsp;
        <i>37</i>&nbsp; /**
        <i>38</i>&nbsp; * &lt;code&gt;true&lt;/code&gt; iff this UI has the default buttons.
        <i>39</i>&nbsp; */
        <i>40</i>&nbsp; private boolean defaultButtons;
        <i>41</i>&nbsp;
        <i>42</i>&nbsp; /**
        <i>43</i>&nbsp; * Way to format the score.
        <i>44</i>&nbsp; */
        <b class="nc"><i>45</i>&nbsp; private ScoreFormatter scoreFormatter = null;</b>
        <i>46</i>&nbsp;
        <i>47</i>&nbsp; /**
        <i>48</i>&nbsp; * Creates a new Pac-Man UI builder without any mapped keys or buttons.
        <i>49</i>&nbsp; */
        <b class="nc"><i>50</i>&nbsp; public PacManUiBuilder() {</b>
        <b class="nc"><i>51</i>&nbsp; this.defaultButtons = false;</b>
        <b class="nc"><i>52</i>&nbsp; this.buttons = new LinkedHashMap&lt;&gt;();</b>
        <b class="nc"><i>53</i>&nbsp; this.keyMappings = new HashMap&lt;&gt;();</b>
        <i>54</i>&nbsp; }
        <i>55</i>&nbsp;
        <i>56</i>&nbsp; /**
        <i>57</i>&nbsp; * Creates a new Pac-Man UI with the set keys and buttons.
        <i>58</i>&nbsp; *
        <i>59</i>&nbsp; * @param game
        <i>60</i>&nbsp; * The game to build the UI for.
        <i>61</i>&nbsp; * @return A new Pac-Man UI with the set keys and buttons.
        <i>62</i>&nbsp; */
        <i>63</i>&nbsp; public PacManUI build(final Game game) {
        <b class="nc"><i>64</i>&nbsp; assert game != null;</b>
        <i>65</i>&nbsp;
        <b class="nc"><i>66</i>&nbsp; if (defaultButtons) {</b>
        <b class="nc"><i>67</i>&nbsp; addStartButton(game);</b>
        <b class="nc"><i>68</i>&nbsp; addStopButton(game);</b>
        <i>69</i>&nbsp; }
        <b class="nc"><i>70</i>&nbsp; return new PacManUI(game, buttons, keyMappings, scoreFormatter);</b>
        <i>71</i>&nbsp; }
        <i>72</i>&nbsp;
        <i>73</i>&nbsp; /**
        <i>74</i>&nbsp; * Adds a button with the caption {@value #STOP_CAPTION} that stops the
        <i>75</i>&nbsp; * game.
        <i>76</i>&nbsp; *
        <i>77</i>&nbsp; * @param game
        <i>78</i>&nbsp; * The game to stop.
        <i>79</i>&nbsp; */
        <i>80</i>&nbsp; private void addStopButton(final Game game) {
        <b class="nc"><i>81</i>&nbsp; assert game != null;</b>
        <i>82</i>&nbsp;
        <b class="nc"><i>83</i>&nbsp; buttons.put(STOP_CAPTION, game::stop);</b>
        <i>84</i>&nbsp; }
        <i>85</i>&nbsp;
        <i>86</i>&nbsp; /**
        <i>87</i>&nbsp; * Adds a button with the caption {@value #START_CAPTION} that starts the
        <i>88</i>&nbsp; * game.
        <i>89</i>&nbsp; *
        <i>90</i>&nbsp; * @param game
        <i>91</i>&nbsp; * The game to start.
        <i>92</i>&nbsp; */
        <i>93</i>&nbsp; private void addStartButton(final Game game) {
        <b class="nc"><i>94</i>&nbsp; assert game != null;</b>
        <i>95</i>&nbsp;
        <b class="nc"><i>96</i>&nbsp; buttons.put(START_CAPTION, game::start);</b>
        <i>97</i>&nbsp; }
        <i>98</i>&nbsp;
        <i>99</i>&nbsp; /**
        <i>100</i>&nbsp; * Adds a key listener to the UI.
        <i>101</i>&nbsp; *
        <i>102</i>&nbsp; * @param keyCode
        <i>103</i>&nbsp; * The key code of the key as used by {@link java.awt.event.KeyEvent}.
        <i>104</i>&nbsp; * @param action
        <i>105</i>&nbsp; * The action to perform when the key is pressed.
        <i>106</i>&nbsp; * @return The builder.
        <i>107</i>&nbsp; */
        <i>108</i>&nbsp; public PacManUiBuilder addKey(Integer keyCode, Action action) {
        <b class="nc"><i>109</i>&nbsp; assert keyCode != null;</b>
        <b class="nc"><i>110</i>&nbsp; assert action != null;</b>
        <i>111</i>&nbsp;
        <b class="nc"><i>112</i>&nbsp; keyMappings.put(keyCode, action);</b>
        <b class="nc"><i>113</i>&nbsp; return this;</b>
        <i>114</i>&nbsp; }
        <i>115</i>&nbsp;
        <i>116</i>&nbsp; /**
        <i>117</i>&nbsp; * Adds a button to the UI.
        <i>118</i>&nbsp; *
        <i>119</i>&nbsp; * @param caption
        <i>120</i>&nbsp; * The caption of the button.
        <i>121</i>&nbsp; * @param action
        <i>122</i>&nbsp; * The action to execute when the button is clicked.
        <i>123</i>&nbsp; * @return The builder.
        <i>124</i>&nbsp; */
        <i>125</i>&nbsp; public PacManUiBuilder addButton(String caption, Action action) {
        <b class="nc"><i>126</i>&nbsp; assert caption != null;</b>
        <b class="nc"><i>127</i>&nbsp; assert !caption.isEmpty();</b>
        <b class="nc"><i>128</i>&nbsp; assert action != null;</b>
        <i>129</i>&nbsp;
        <b class="nc"><i>130</i>&nbsp; buttons.put(caption, action);</b>
        <b class="nc"><i>131</i>&nbsp; return this;</b>
        <i>132</i>&nbsp; }
        <i>133</i>&nbsp;
        <i>134</i>&nbsp; /**
        <i>135</i>&nbsp; * Adds a start and stop button to the UI. The actual actions for these
        <i>136</i>&nbsp; * buttons will be added upon building the UI.
        <i>137</i>&nbsp; *
        <i>138</i>&nbsp; * @return The builder.
        <i>139</i>&nbsp; */
        <i>140</i>&nbsp; public PacManUiBuilder withDefaultButtons() {
        <b class="nc"><i>141</i>&nbsp; defaultButtons = true;</b>
        <b class="nc"><i>142</i>&nbsp; buttons.put(START_CAPTION, null);</b>
        <b class="nc"><i>143</i>&nbsp; buttons.put(STOP_CAPTION, null);</b>
        <b class="nc"><i>144</i>&nbsp; return this;</b>
        <i>145</i>&nbsp; }
        <i>146</i>&nbsp;
        <i>147</i>&nbsp; /**
        <i>148</i>&nbsp; * Provide formatter for the score.
        <i>149</i>&nbsp; *
        <i>150</i>&nbsp; * @param scoreFormatter
        <i>151</i>&nbsp; * The score formatter to be used.
        <i>152</i>&nbsp; *
        <i>153</i>&nbsp; * @return The builder.
        <i>154</i>&nbsp; */
        <i>155</i>&nbsp; public PacManUiBuilder withScoreFormatter(ScoreFormatter scoreFormatter) {
        <b class="nc"><i>156</i>&nbsp; this.scoreFormatter = scoreFormatter;</b>
        <b class="nc"><i>157</i>&nbsp; return this;</b>
        <i>158</i>&nbsp; }
        <i>159</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
